package org.link.zuoye2;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 
 * project_name:ZuoYe
 * package_name:org.link.zuoye2
 * file_name:Date1.java
 * @author Link
 * create_date:2015年8月11日 下午3:09:47
 * modified_date:
 * description:日期与星期之间的切换
 *
 */
public class Date1 extends JFrame{
	
	private JLabel label1,label2;
	private JTextField field1,field2;
	private JButton bt;
	
	public Date1(){
		this.setSize(300,200);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation((int)((screenSize.getWidth()-frameSize.getWidth())/2), (int)(screenSize.getHeight()-frameSize.getHeight())/2);
		this.setTitle("日期转换");
		ImageIcon tp = new ImageIcon(Date1.class.getResource("/image/4444.jpg"));
		this.setIconImage(tp.getImage());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭同时关闭进程
		this.setResizable(false);//设置窗体不可放大
		
		this.setLayout(null);
		
		label1 = new JLabel("输入日期");
		label1.setBounds(55, 20, 80, 20);
		this.add(label1);
		
		label2 = new JLabel("星期几");
		label2.setBounds(210, 20, 50, 20);
		this.add(label2);
		
		field1 = new JTextField();
		field1.setBounds(30, 45, 100, 30);
		field1.setHorizontalAlignment(JTextField.CENTER);//设置文本框居中
		field1.setFont(new Font("微软雅黑",Font.ROMAN_BASELINE,12));
		this.add(field1);
		
		field2 = new JTextField();
		field2.setBounds(180, 45, 100, 30);
		field2.setHorizontalAlignment(JTextField.CENTER);//设置文本框居中
		field2.setFont(new Font("微软雅黑",Font.ROMAN_BASELINE,14));
		this.add(field2);
		
		bt = new JButton("转换");
		bt.setBounds(80, 100, 100, 35);
		bt.setMargin(new Insets(0,0,0,0));//设置按钮的边缘空余部分为0
		this.add(bt);
		bt.addActionListener(new ActionListener(){
			
			@Override
			public void actionPerformed(ActionEvent e)  {
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date d = null;
				try {
					d = sdf.parse(field1.getText());//解析的字符串的形式需满足SimpleDateFormat格式的要求
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if(d.toString().substring(0, 2).equals("Mo")){//截取字符串d的前两位进行判断
					field2.setText("星期一");
				}else if(d.toString().substring(0, 2).equals("Tu")){
					field2.setText("星期二");
				}else if(d.toString().substring(0, 2).equals("We")){
					field2.setText("星期三");
				}else if(d.toString().substring(0, 2).equals("Th")){
					field2.setText("星期四");
				}else if(d.toString().substring(0, 2).equals("Fr")){
					field2.setText("星期五");
				}else if(d.toString().substring(0, 2).equals("Sa")){
					field2.setText("星期六");
				}else{
					field2.setText("星期天");
				}
				
			}
		});
		
	}
	
	public static void main(String[] args) throws ParseException{
		Date1 d1 = new Date1();
		d1.setVisible(true);
		
		
		
	}
	
}
